﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Traders.Controllers;

namespace Traders.Views
{
    public partial class TradersWindow : Form
    {
        private TradersController controller;
        
        public void ShowProgressFormWhileBackgroundWorkerRuns(Action<ConnectingProgress> exec)
        {
            ConnectingProgress p = new ConnectingProgress();

            BackgroundWorker b = new BackgroundWorker();

            // set the worker to call your long-running method
            b.DoWork += (object sender, DoWorkEventArgs e) =>
            {
                exec.Invoke(p);
            };

            // set the worker to close your progress form when it's completed
            b.RunWorkerCompleted += (object sender, RunWorkerCompletedEventArgs e) =>
            {
                if (p != null && p.Visible)
                {
                    p.Close();
                    Enabled = true;
                    this.tabControl1.SelectTab(1);
                }
            };

            // now actually show the form
            p.Show(this);
            Enabled = false;

            // this only tells your BackgroundWorker to START working;
            // the current (i.e., GUI) thread will immediately continue,
            // which means your progress bar will update, the window
            // will continue firing button click events and all that
            // good stuff
            b.RunWorkerAsync();
        }

        public TradersWindow()
        {
            controller = new TradersController(this);
            InitializeComponent();
            controller.setBalanceView(this.balanceView1);
            controller.setOpenTradesView(this.openTradesView1);
            
        }

        private void ticks_Tick(object sender, EventArgs e)
        {
            if (controller != null)
                controller.doTicks(1000);
        }



    }
}
